export default defineUnlistedScript(() => {
  console.log(window)
  window.addEventListener('ext-req', async (e) => {
    const { type, data } = e.detail

    switch (type) {
      case 'run-code': {
        let fn = new Function(`return (${data.function})(...arguments)`)

        let rs = await fn(...(data.args ?? []))
        sendExtResponse(type, rs)
        break
      }
      case 'download': {
        console.log(data)
       }

     }
  })

  function sendExtResponse(type, data) {
    window.dispatchEvent(
      new CustomEvent('ext-res', {
        detail: { type, data },
      })
    )
  }
})
